package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.dto.yx7881.core.LieBaoCore;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 7881 属性匹配
 */
@Slf4j
@Service
public class MONI7881Attr implements AdapterAttr {

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String response = HttpRequest.post("https://gw.7881.com/basic/api/game-search").body("{\"hotFlag\":\"\",\"gameType\":\"\",\"key\":\"\",\"px\":\"\"}").execute().body();

        List<JSONObject> jsonObjects = JSONObject.parseArray(JSONObject.parseObject(response).getString("body"), JSONObject.class);

        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {

            GamesRes gamesRes = new GamesRes();
            String gameId = jsonObject.getString("gameId");
            String gameName = jsonObject.getString("gameName");
            gamesRes.setId(gameId);
            gamesRes.setGameName(gameName);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        List<InterServerRes> interServerRes = new ArrayList<>();
        InterServerRes interServer = new InterServerRes();
        interServer.setId("7881不匹配区服,随便勾选一个");
        interServer.setName("7881不匹配区服,随便勾选一个");
        interServerRes.add(interServer);
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> resultList = new ArrayList<>();
        String result = HttpRequest.get("https://gw.7881.com/basic/api/publish-item?gameId=" + gameId + "&gtId=100003").execute().body();
        JSONObject resultJson = JSONObject.parseObject(result);
        List<JSONObject> data = JSONObject.parseArray(resultJson.getString("body"), JSONObject.class);
        for (JSONObject datum : data) {
            InterAttrRes res = new InterAttrRes();
            res.setUuid(UUIDGenerator.generateUuid6());
            if (StringUtils.equals(type, "1")) {
                if (StringUtils.equals(datum.getString("type"), "select")) {
                    res.setId(datum.getString("id"));
                    res.setName(datum.getString("keyname"));
                    res.setRequired(datum.getBoolean("must"));
                    String values = datum.getString("value");
                    List<InterAttrRes> arrayList = new ArrayList<>();
                    List<String> list = Arrays.asList(values.split(","));
                    for (String s : list) {
                        InterAttrRes res1 = new InterAttrRes();
                        res1.setUuid(UUIDGenerator.generateUuid6());
                        res1.setId(s);
                        res1.setName(s);
                        res1.setRequired(false);
                        arrayList.add(res1);
                    }
                    res.setInterServerRes(arrayList);
                    resultList.add(res);
                }
            } else if (StringUtils.equals(type, "2")) {
                // 针对原神 DNF 有多重属性的筛选项
                if (StringUtils.equals(datum.getString("type"), "checkbox")) {
                    if (CollectionUtils.isNotEmpty(datum.getJSONArray("children"))){
                        List<JSONObject> array = JSONObject.parseArray(datum.getString("children"), JSONObject.class);
                        for (JSONObject jsonObject : array) {
                            res.setId(jsonObject.getString("id"));
                            res.setName(jsonObject.getString("keyname"));
                            res.setRequired(jsonObject.getBoolean("must"));
                            String values = jsonObject.getString("value");
                            List<InterAttrRes> arrayList = new ArrayList<>();
                            //  子选项 会有   ;  , 两种分割方式
                            List<String> sList = Arrays.asList(values.split(";"));
                            for (String ss : sList) {
                                List<String> list = Arrays.asList(ss.split(","));
                                for (String s : list) {
                                    InterAttrRes res1 = new InterAttrRes();
                                    res1.setUuid(UUIDGenerator.generateUuid6());
                                    res1.setId(s);
                                    res1.setName(s);
                                    res1.setRequired(false);
                                    arrayList.add(res1);
                                }
                            }
                            res.setInterServerRes(arrayList);
                            resultList.add(res);

                        }
                    }else{
                        res.setId(datum.getString("id"));
                        res.setName(datum.getString("keyname"));
                        res.setRequired(datum.getBoolean("must"));
                        String values = datum.getString("value");
                        List<InterAttrRes> arrayList = new ArrayList<>();
                        List<String> list = Arrays.asList(values.split(","));
                        for (String s : list) {
                            InterAttrRes res1 = new InterAttrRes();
                            res1.setUuid(UUIDGenerator.generateUuid6());
                            res1.setId(s);
                            res1.setName(s);
                            res1.setRequired(false);
                            arrayList.add(res1);
                        }
                        res.setInterServerRes(arrayList);
                        resultList.add(res);
                    }
                }
            } else if (StringUtils.equals(type, "3")) {
                if (StringUtils.equals(datum.getString("type"), "text")|| StringUtils.equals(datum.getString("type"), "password")) {
                    res.setId(datum.getString("id"));
                    res.setName(datum.getString("keyname"));
                    res.setRequired(datum.getBoolean("must"));
                    resultList.add(res);
                }
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        Map<String,Object> params = new HashMap<>();
        params.put("","");
        String response = HttpRequest.post("https://gw.7881.com/basic/api/game-search").body("{\"hotFlag\":\"\",\"gameType\":\"\",\"key\":\"\",\"px\":\"\"}").execute().body();
        System.out.println(response);
    }
    static String getGameInfo(String gameid, SalePartner salePartner) {
        String result = null;
        try {
            result = LieBaoCore.get_game_info(gameid, salePartner);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}